/*
 * ForSaleDAO.java
 *
 * Created on March 26, 2007, 1:45 PM
 *
 
 */

package edu.byu.isys413.mystuff.sales;

import edu.byu.isys413.mystuff.*;
import edu.byu.isys413.mystuff.DataException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;



/**
 *
 * @author Gabe
 */
public class forSaleDAO extends PhysicalDAO{
    
    ///////////////////////////////////////
    ///   Singleton pattern
    private static forSaleDAO instance = null;
    
    /** Creates a new instance of ForSaleDAO */
    public forSaleDAO() {
    }
    
    
    /**
     * This gets an instance of the forsaledao (because it is not abstract
     */
    public static synchronized forSaleDAO getInstance() {
        if (instance == null) {
            instance = new forSaleDAO();
        }
        return instance;
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    /////////   CREATE
    
    public ForSaleBO create() {
        String id="";
        try {
            id = GUID.generate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        ForSaleBO salesBO = new ForSaleBO(id);
        
        Cache c = Cache.getInstance();
        
        c.put(salesBO.getId(), salesBO);
        
        return salesBO;
    }
    
    public synchronized ForSaleBO read(String id) throws SQLException, DataException{
        Cache c = Cache.getInstance();
        Connection conn = null;
        ForSaleBO forSale = (ForSaleBO)c.get(id);
        if (forSale != null){
            return forSale;                                                        // if so, return it immediately
        } else{
            try {
                conn = ConnectionPool.getInstance().get();
                //when you call readSku you need to return a productBO
                forSale = (ForSaleBO)read(id, conn);
                ConnectionPool.getInstance().release(conn);
            }catch (Exception e) {
                e.printStackTrace();
                
            }
        }
        
        return forSale;
    }
    
    
    public synchronized ProductBO read(String id,Connection conn) throws SQLException, DataException{
        // check to see if id in the cache
        Cache c = Cache.getInstance();
        ForSaleBO forSale = (ForSaleBO)c.get(id);
        if (forSale != null){
            return forSale;                                                        // if so, return it immediately
        }
        //put it to the cache to allow the super read to get access
        forSale  = new ForSaleBO(id);//the product id is being set in the forsale bo
        
        //put in cache
        c.put(id, forSale);
        
        //we are calling super.read and passing it the product id...we want it to return a forsaleBO that has hidden access to phyBO and productBO
        forSale = (ForSaleBO)super.read(id, conn);
        System.out.println("The price is: " + forSale.getPrice());
        System.out.println("That means you have a product you bastage");
        PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM For_Sale WHERE ProductId=?");
        //give the value of id
        pstmt.setString(1,id);
        ResultSet rs = pstmt.executeQuery();
        
        int newOrUsed;
        if (rs.next()) {
            newOrUsed= rs.getInt("New_Used");
            
            if(newOrUsed == 0){
                forSale.setNewOrUsed(false);
            } else{
                System.out.println("New or used is 1");
                forSale.setNewOrUsed(true);
            }
            
            forSale.setAlreadyInDatabase(true);
            forSale.setDirty(false);
            
            // save to the cache
            c.put(forSale.getProductID(), forSale);
            
            //return forsale bo with all other bo and hidden info to my tester class
            
        }
        //return forsale bo with all other bo and hidden info to my tester class
        return forSale;
    }
    public synchronized ArrayList<ProductBO> readAll() throws SQLException, DataException{
        ArrayList<ProductBO> list = new ArrayList<ProductBO>();
        
        return list;
    }
    
//    }
    
    public List<String[]> getAll() throws SQLException, DataException {
        
        
        List<String[]> list = new ArrayList<String[]>();
        
        Connection conn = null;
        
        try {
            // get the connection
            conn = ConnectionPool.getInstance().get();
            
            
            Statement stmt = conn.createStatement();
            
            ResultSet rs = stmt.executeQuery("SELECT * From Conceptual_Product");
            
            // while loop to populate the list from the results
            while(rs.next()){
                String[] temp = new String[2];
                temp[0] = rs.getString("productId");
                temp[1] = rs.getString("productName");
                temp[2] = rs.getString("description");
                temp[3] = rs.getString("averageCost");
                list.add(temp);
            }
            
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new DataException("Conceptual Product information not retrieved");
        }
        try {
            // release my connection
            ConnectionPool.getInstance().release(conn);
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        }
        // return the list
        return list;
    }
    
    
    
    public synchronized void insert(ProductBO productBO, Connection conn) throws SQLException, DataException{
//        // do the insert SQL statement
//        PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Conceptual_Product (,lname,address,city,state,zip,phone,email,custid) VALUES (?,?,?,?,?,?,?,?,?)");
//        try {
//            pstmt.setString(1,cust.getFname());
//            pstmt.setString(2,cust.getLname());
//
//            pstmt.executeUpdate();
//            pstmt.close();
//            conn.commit();
//        } catch (SQLException ex) {
//            throw ex;
//        }
//        // tell the object that it's now in the db (so we call update next time not insert)
//        cust.setAlreadyInDB(true);
    }
    
    public ProductBO readSku(String sku, Connection conn) throws SQLException, DataException {
        ForSaleBO bo = null;
        return bo;
    }
    
}

